/*
 * $Id$
 */
package corejs;

import java.util.Iterator;

/**
 *
 * @author myang
 */
public abstract class Scanner implements Iterable<Token> {

   protected Source _src;
   private Token _token;

   public Scanner(final Source src) {
      _src = src;
   }

   public Token currentToken() {
      return _token;
   }

   public Token nextToken() {
      _token = extractToken();
      return _token;
   }

   /**
    * 
    * @return 
    */
   public char currentChar() { return _src.current(); }

   /**
    * 
    * @return 
    */
   public char nextChar() { return _src.next(); }

   /**
    * 
    * @return 
    */
   public char prevChar() { return _src.prev(); }

   /**
    * 
    * @return 
    */
   public char peekChar() { return _src.peek(); }

   /**
    * 
    * @return 
    */
   protected abstract Token extractToken();

   @Override
   public Iterator<Token> iterator() {
      return new Iterator<Token>() {
         @Override
         public boolean hasNext() {
            return _token != Token.EOF;
         }

         @Override
         public Token next() {
            return nextToken();
         }
         
         @Override
         public void remove() {
           
         }
      };
   }
}
